﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;


namespace ASteroids
{
    class CollisionHandler
    {
        private AsteroidManager asteroidManager;
        private List<PlayerObject> playerManager;
        private Vector2 offScreen = new Vector2(-500, -500);
        private Vector2 shotToAsteroidImpact = new Vector2(0, -20);
        private int enemyPointValue = 100;


        public CollisionHandler(
            AsteroidManager asteroidManager,
            List<PlayerObject> playerManager)
        {
            this.asteroidManager = asteroidManager;
            this.playerManager = playerManager;
           
        }

        private void checkShotToAsteroidCollisions()
        {
            foreach (PlayerObject player in playerManager)
            {
                foreach (Sprite shot in player.PlayerShotManager.Shots)
                {
                    foreach (Sprite asteroid in asteroidManager.Asteroids)
                    {
                        if (shot.IsCircleColliding(
                        asteroid.Center,
                        asteroid.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            asteroid.Velocity += shotToAsteroidImpact;
                        }
                    }
                }
            }
        }

        private void checkAsteroidToPlayerCollisions()
        {
            foreach (PlayerObject player in playerManager)
            {
                foreach (Sprite asteroid in asteroidManager.Asteroids)
                {
                    if (asteroid.IsCircleColliding(
                    player.playerSprite.Center,
                    player.playerSprite.CollisionRadius))
                    {

                        asteroid.Location = offScreen;
                        player.PlayerShield -= asteroidManager.Damage;
                    }
                }
            }
        }

        public void CheckCollisions()
        {
            foreach (PlayerObject player in playerManager)
            {
                checkShotToAsteroidCollisions();
                if (!player.Destroyed)
                {
                    checkAsteroidToPlayerCollisions();
                }
            }
        }

    }
}
